package cn.wq3stone.filter;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author wq3stone
 */

public class CharacterEncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init方法被执行！");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=gbk");
        PrintWriter out = servletResponse.getWriter();
        out.println("<h1>进入doFilter</h1>" );
        System.out.println("doFilter执行之前");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("doFilter执行之后");
    }

    @Override
    public void destroy() {
        System.out.println("destroy方法被执行！");
    }
}
